<!--
  -    Copyright (c) 2018-2025, lengleng All rights reserved.
  -
  - Redistribution and use in source and binary forms, with or without
  - modification, are permitted provided that the following conditions are met:
  -
  - Redistributions of source code must retain the above copyright notice,
  - this list of conditions and the following disclaimer.
  - Redistributions in binary form must reproduce the above copyright
  - notice, this list of conditions and the following disclaimer in the
  - documentation and/or other materials provided with the distribution.
  - Neither the name of the pig4cloud.com developer nor the names of its
  - contributors may be used to endorse or promote products derived from
  - this software without specific prior written permission.
  - Author: lengleng (wangiegie@gmail.com)
  -->
<template>
	<div class="execution">
		<basic-container>
			<avue-crud
				ref="form"
				v-model="form"
				:page="page"
				:data="tableData"
				:permission="permissionList"
				:table-loading="tableLoading"
				:option="tableOption"
				:before-close="beforeClose"
				@on-load="getList"
				@search-change="searchChange"
				@refresh-change="refreshChange"
				@size-change="sizeChange"
				@current-change="currentChange"
				@row-update="handleUpdate"
				@row-save="handleSave"
				@row-del="rowDel"
			>
				<template slot-scope="scope" slot="menu">
					<el-button
						style="background: none; border: none;color:#409EFF;"
						size="small"
						plain
						@click.stop="seeDetail(scope.row, scope.index)"
					>查看详情
					</el-button
					>
				</template>
			</avue-crud>
		</basic-container>
	</div>
</template>

<script>
import {fetchList, getObj, addObj, putObj, delObj} from '@/api/circletasklearningbackage'
import {userBytask} from '@/api/circlelearningbackageDetail'
import {tableOption} from '@/const/crud/circletasklearningbackage'
import {mapGetters} from 'vuex'

export default {
	name: 'circletasklearningbackage',
	data() {
		return {
			dialogFlag: false,
			form: {},
			searchForm: {},
			tableData: [],
			page: {
				total: 0, // 总页数
				currentPage: 1, // 当前页数
				pageSize: 20 // 每页显示多少条
			},
			tableLoading: false,
			tableOption: tableOption,
			levelOne: []
		}
	},
	watch: {
		'form.learnBacks': {
			handler(val) {
				var text2 = this.findObject(this.tableOption.column, 'userIds')
				console.log('----------------------')
				console.log(val, this.form)
				if (val && val.length) {
					text2.disabled = false;
					this.form.userIds = ''
				} else {
					text2.disabled = true;
				}
			},
			immediate: true
		},
		// 'form.endTime': {
		//   handler(val) {
		//     val = val.replace(/-/g, '/');
		//     let time = new Date(val);
		//     let dangqian = new Date();
		//     console.log(val, this.form)
		//     if (time < dangqian) {
		//       this.$message.warning('结束时间不能小于当前时间！！！')
		//     }
		//   },
		//   immediate: true
		// },
	},
	computed: {
		...mapGetters(['permissions']),
		permissionList() {
			return {
				addBtn: this.vaildData(this.permissions.circleTaskLearningbackage_circletasklearningbackage_add, false),
				delBtn: this.vaildData(this.permissions.circleTaskLearningbackage_circletasklearningbackage_del, false),
				editBtn: this.vaildData(this.permissions.circleTaskLearningbackage_circletasklearningbackage_edit, false)
			};
		}
	},

	mounted() {
		var learnBacks = this.findObject(this.tableOption.column, 'learnBacks')
		learnBacks.change = (row) => {
			console.log(row)
			let column = row.column;
			let value = row.value;
			console.log(value)
			if (value) {
				userBytask({
					learnBack: value.join(',')
				}).then(res => {
					let circleorganizational = res.data.data.circleOrganizationalVoList
					// console.log(this.circleorganizational)
					circleorganizational.map(r => {
						this.levelOne.push(r.id);
					})
					console.log(this.tableData)
					this.tableOption.column.map(r => {
						if (r.prop === 'userIds') {
							r.dicData = circleorganizational
						}
						return r;
					})
				})
			}

		}
		var endTime = this.findObject(this.tableOption.column, 'endTime')
		endTime.blur = (res) => {
			console.log('---------------------------', res);
			let val = res.value;
			val = val.replace(/-/g, '/');
			let time = new Date(val);
			let dangqian = new Date();
			console.log(val, this.form)
			if (time < dangqian) {
				this.$message.error('结束时间不能小于当前时间！！！')
			}
		};
	},
	methods: {
		beforeClose(done, type) {
			if (type == "add" || type == "edit") {
				if (this.dialogFlag == true) {
					done()
				}
				if (this.dialogFlag == false) {
					this.$confirm("可能有未保存的数据，是否关闭", '提示', {
						confirmButtonText: '确定',
						cancelButtonText: '取消',
						type: 'warning'
					}).then(data => {
						done()
					}).catch(_ => {
						// done()
					});
				}
			} else {
				done()
			}

		},
		seeDetail(row) {
			console.log(row)
			this.$router.push({
				path: "/learningbackagedetail/index",
				query: {
					id: row.id
				}
			});
		},
		getList(page, params) {
			this.tableLoading = true;
			console.log(this.searchForm)
			fetchList(Object.assign({
				current: page.currentPage,
				size: page.pageSize
			}, params, this.searchForm)).then(response => {
				this.tableData = response.data.data.records
				this.page.total = response.data.data.total
				this.tableLoading = false
			}).catch(() => {
				this.tableLoading = false
			})
		},
		rowDel: function (row, index) {
			this.$confirm('是否确认删除ID为' + row.id, '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(function () {
				return delObj(row.id)
			}).then(data => {
				this.$message.success('删除成功')
				this.getList(this.page)
			})
		},
		handleUpdate: function (row, index, done, loading) {
			putObj(row).then(data => {
				this.$message.success('修改成功')
				this.dialogFlag = true
				done()
				this.getList(this.page)
			}).catch(() => {
				loading();
			});
			setTimeout(() => {
				vm.dialogFlag = false
			}, 500)
		},
		handleSave: function (row, done, loading) {
			var newIdsArr = [];
			debugger
			row.userIds.split(',').map(r => {
				if (this.levelOne.indexOf(r) === -1) {
					newIdsArr.push(r);
				}
			})
			console.log(row)
			let _d = {};
			_d.userIds = newIdsArr.join(',');
			_d.endTime = row.endTime;
			_d.name = row.name;
			_d.learnBacks = row.learnBacks.join(',');

			addObj(_d).then(data => {
				this.$message.success('添加成功')
				this.dialogFlag = true
				done()
				this.getList(this.page)
			}).catch(() => {
				loading();
			});
			setTimeout(() => {
				vm.dialogFlag = false
			}, 500)
		},
		sizeChange(pageSize) {
			this.page.pageSize = pageSize
		},
		currentChange(current) {
			this.page.currentPage = current
		},
		searchChange(form, done) {
			this.searchForm = form
			this.page.currentPage = 1;
			console.log(form);
			if (form.datetime) {
				form.startTime = form.datetime[0];
				form.endTime = form.datetime[1];
			}
			this.getList(this.page, form)
			done()
		},
		refreshChange() {
			this.getList(this.page)
		}
	}
}
</script>
